﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using NLog;
using PdfSharp.Pdf.Printing;
using RestSharp;
using RestSharp.Extensions;
using Spire.Pdf;

namespace Cef.Utility.Helper
{
    public static class PrinterHelper
    {
        private static NLog.Logger logger = LogManager.GetCurrentClassLogger();

        private static string defaultPrinterName= DefaultPrinter();
        //[DllImport("winspool.drv")]
        //public static extern bool SetDefaultPrinter(String Name);

        //调用win api将指定名称的打印机设置为默认打印机
        public static List<string> GetLocalPrinters()
        {
            List<string> fPrinters = new List<string>();
            defaultPrinterName = DefaultPrinter();
            if (defaultPrinterName != null)
            {
                fPrinters.Add(defaultPrinterName); //默认打印机始终出现在列表的第一项
            }
            foreach (string fPrinterName in PrinterSettings.InstalledPrinters)
            {
                if (!fPrinters.Contains(fPrinterName))
                {
                    fPrinters.Add(fPrinterName);
                }
            }
            return fPrinters;
        }

        public static bool SetDefaultPrinter(string printerName)
        {
            return DynamicConfigHelper.SetDefaultPrinter(printerName);
        }

        public static string DefaultPrinter()
        {
            return DynamicConfigHelper.GetDefaultPrinter();
        }
        public static bool DownLoadAndPrint(string pdfUrl)
        {
            var path = Directory.GetCurrentDirectory() + $"\\TempFile";
            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                path = path + $"\\{Guid.NewGuid()}.pdf";

                var client = new RestClient(pdfUrl);
                var request = new RestRequest(Method.GET);
                var data = client.DownloadData(request);
                data.SaveAs(path);
                return Print(path,true);
                //return PdfAdobePrint(path);
                //return pdfPrint(path);
            }
            catch (Exception e)
            {
                logger.Error(e);
                FileAttributes attr = File.GetAttributes(path);
                if (attr != FileAttributes.Directory)
                {
                    File.Delete(path);
                }
            }

            return false;
        }
        private static bool pdfPrint(string filePath)
        {
            PrintDocument pd = new PrintDocument();
            Process p = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.CreateNoWindow = true;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.UseShellExecute = true;
            startInfo.FileName = filePath;
            startInfo.Verb = "print";
            startInfo.Arguments = @"/p /h \" + filePath + "\"\"" + DynamicConfigHelper.GetDefaultPrinter() + "\"";
            //startInfo.Arguments = @"/p /h \" + filePath + "\"\"" + pd.PrinterSettings.PrinterName + "\"";
            p.StartInfo = startInfo;
            p.Start();
            p.WaitForExit();
            return true;

        }
        //private static bool PdfAdobePrint(string filePath)
        //{
        //    filePath ="C:\\Users\\Kenny\\OneDrive\\iHive\\微信号\\微信公众平台认证公函.pdf";
        //    PdfFilePrinter.AdobeReaderPath = "C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\AcroRd32.exe";
        //    PdfFilePrinter printer = new PdfFilePrinter(filePath, DynamicConfigHelper.GetDefaultPrinter());
        //    printer.Print();
        //    return true;
        //}
        public static bool Print(string path,bool isDelete=false)
        {

            try
            {
                PdfDocument pdf = new PdfDocument();
                //var path = Directory.GetCurrentDirectory() + "\\EULA.pdf";
                //path = "C:\\Users\\Kenny\\OneDrive\\iHive\\微信号\\微信公众平台认证公函.pdf";
                pdf.LoadFromFile(path);

                pdf.PrintDocument.PrinterSettings.PrinterName = DynamicConfigHelper.GetDefaultPrinter();
                pdf.PrintDocument.PrintController = new StandardPrintController();
                pdf.PrintDocument.Print();
                File.Delete(path);
                return true;
            }
            catch (Exception exception)
            {
                logger.Error(exception);
            }

            return false;
        }


    }
    
}
